<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="utf-8">
<title>SmartPing Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="css/font-awesome.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/pages/dashboard.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <a class="brand" href="index.html">SmartPing Dashbord </a>
      <div class="nav-collapse">
        <ul class="nav pull-right">
          <li></li>
        </ul>
      </div>
      <!--/.nav-collapse -->
    </div>
    <!-- /container --> 
  </div>
  <!-- /navbar-inner --> 
</div>
<!-- /navbar -->
<div class="subnavbar">
  <div class="subnavbar-inner">
    <div class="container">
      <ul class="mainnav">
        <li class="active"><a href="index.html"><i class="icon-mail-forward"></i><span>Forward Ping</span> </a> </li>
        <li><a href="reverse.html"><i class="icon-mail-reply"></i><span>Reverse Ping</span> </a> </li>
        <li><a href="topology.html"><i class="icon-bar-chart"></i><span>Topology</span> </a> </li>
        <li><a href="alerts.html"><i class="icon-warning-sign"></i><span>Alerts</span> </a> </li>
        <li><a href="config.html"><i class="icon-cog"></i><span>Config</span> </a> </li>
       <li></li>
      </ul>
    </div>
    <!-- /container --> 
  </div>
  <!-- /subnavbar-inner --> 
</div>
<!-- /subnavbar -->
<div class="main" style="margin-top:-20px;">
  <div class="main-inner">
    <div class="container">

      <div class="row">
        <div  id="main" class="span10">
          <div id="echarts" style="width:100%;"></div>
        </div>
        <!-- /span10 -->
        <div class="span2">
          <div class="widget widget-table action-table">
            <div class="widget-header"> <i class="icon-th-list"></i>
              <h3>Agent List</h3>
            </div>
            <!-- /widget-header -->
            <div class="widget-content">
              <table class="table table-striped table-bordered">
                <thead>
                <tr>
                  <th> Agent </th>
                </tr>
                </thead>
                <tbody class="agentlist">

                </tbody>
              </table>
            </div>
            <!-- /widget-content -->
          </div>
        </div>
      </div>

    </div>
    <!-- /container --> 
  </div>
  <!-- /main-inner --> 
</div>
<!-- /main -->



<div class="footer">
  <div class="footer-inner">
    <div class="container">
      <div class="row">
        <div class="span12"> &copy; 2017 <a target="_blank" href="http://smartping.org" >SmartPing.org</a> <span style="float:right" id="verion"></span></div>
        <!-- /span12 --> 
      </div>
      <!-- /row --> 
    </div>
    <!-- /container --> 
  </div>
  <!-- /footer-inner --> 
</div>

<div id="charts" class="modal fade" tabindex="-1" style="width: 830px;height:500px;">
  <input type="hidden" id="pannelurl" value=""/>
  <div class="modal-dialog">
    <div class="modal-content" >
        <div style="text-align:center;padding-top:10px;">
          <b>StartTime:</b>&nbsp;<input id="starttime" type='text'  onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})" style="width:120px;" >
          <b>EndTime:</b>&nbsp;<input id="endtime" type='text'  onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})"  style="width:120px;" >
          &nbsp;<input class="sgraph" type="button" value="Submit" />
        </div>
        <div class="modal-body" id="pannel-show" style="width: 800px;height:500px;">
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- /footer --> 
<!-- Le javascript
================================================== --> 
<!-- Placed at the end of the document so the pages load faster --> 
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/echarts.min.js"></script>
<script language="JavaScript" type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script>
<script>
    $.getJSON("/api/config.json",function(result){
        $("#verion").html("Version: "+result["Ver"])
        $.each(result["Targets"], function(i, field){
            if(field.Type=="CS") {
                if(field.Addr==result["Ip"]){
                    return true;
                }
                $(".agentlist").append("<tr><td><i class='icon-spinner icon-spin animated alerticon-" + field.Name + "'></i>&nbsp;<a target='_blank' href='http://" + field.Addr + ":" + result["Port"] + "'>" + field.Name + "</a></td></tr>");
            }
        });
        optmini={
            title:{
                text: '',
                'x':'center'
            },
            tooltip: {},
            grid: {
                left: '3%',
                right: '3%',
                bottom: '3%',
                top: '3%',
                containLabel: true
            },
            xAxis: {
                data: []
            },
            yAxis: [{
                type: 'value',
                position: 'left'
            }, {
                type: 'value',
                min: 0,
                max: 100,
                position: 'right'
            }],
            series: [
                {
                    name: 'avgDelay',
                    type: 'line',
                    data: [],
                    itemStyle: {
                        normal: {
                            color : '#00CC66'
                        }
                    },
                    animation: false,
                    areaStyle: {
                        normal: {}
                    },
                    lineStyle: {
                        normal: {
                            width: 0
                        }
                    }
                },
                {
                    name: 'lossPk',
                    type: 'line',
                    yAxisIndex: 1,
                    data: [],
                    itemStyle: {
                        normal: {
                            color : '#FF0000'
                        }
                    },
                    animation: false,
                    areaStyle: {
                        normal: {}
                    },
                    lineStyle: {
                        normal: {
                            width: 0
                        }
                    }
                }]
        }
        $.each(result["Targets"], function(i, field){
            if(field.Addr==result["Ip"]){
                return true;
            }
            $("#main").append("<div id='"+field.Name+"_pannel' style='float:left;width:30.3%;height:130px;margin-right:3%;margin-top:20px;'  class='showcharts' apiurl='/api/ping.json?ip="+field.Addr+"'></div>");
            var chart = echarts.init(document.getElementById(field.Name+"_pannel"));
            chart.setOption(optmini);
            chart.setOption({title:{text:result["Name"]+"->"+field.Name,textStyle:{'fontSize':13}}});
            $.ajax({
                dataType: "json",
                url: '/api/ping.json?ip='+field.Addr,
                success: function (data) {
                    $(".alerticon-"+field.Name+"").remove();
                    chart.setOption({
                        xAxis: {
                            data: data.lastcheck
                        },
                        series: [{
                            name: 'avgDelay',
                            data: data.avgdelay
                        },{
                            name: 'lossPk',
                            data: data.losspk
                        }]
                    });
                },
                timeout: result["Timeout"]*1000
            }).fail( function( xhr, status ) {
                $(".alerticon-"+field.Name+"").removeClass("icon-spinner").removeClass("icon-spin").removeClass("animated");
                $(".alerticon-"+field.Name+"").addClass("icon-warning-sign");
            });
        });
        $("#main").on("click",".showcharts",function(){
            var modalWidth = $("#charts").width();
            var left = "-" + parseInt(modalWidth) / 2 + "px";
            $('#charts').modal('show').css({"margin-left":left});
            url=$(this).attr("apiurl");
            $("#pannelurl").attr("value",url);
            $.get(url).done(function (data) {
                //var data = JSON.parse(data);
                myChart.setOption({
                    xAxis: {
                        data: data.lastcheck
                    },
                    series: [{
                        name: 'maxDelay',
                        data: data.maxdelay
                    },
                        {
                            name: 'minDelay',
                            data: data.mindelay
                        },
                        {
                            name: 'avgDelay',
                            data: data.avgdelay
                        },
                        {
                            name: 'lossPk',
                            data: data.losspk
                        }]
                });
            });
        });
    });
    $(".sgraph").click(function(){
        start   = $("#starttime").val();
        endtime = $("#endtime").val();
        url = $("#pannelurl").attr("value");
        $.get(url+"&starttime="+start+"&endtime="+endtime).done(function (data) {
            //console.log(data)
            //var data = JSON.parse(data);
            myChart.setOption({
                xAxis: {
                    data: data.lastcheck
                },
                series: [{
                    name: 'maxDelay',
                    data: data.maxdelay
                },
                    {
                        name: 'minDelay',
                        data: data.mindelay
                    },
                    {
                        name: 'avgDelay',
                        data: data.avgdelay
                    },
                    {
                        name: 'lossPk',
                        data: data.losspk
                    }]
            });
        });
    });
    opt={
        title: {
            text: ''
        },
        legend: {
            data:['maxDelay','avgDelay','minDelay','lossPk'],
            selected: {
                'maxDelay' : false,
                'minDelay' : false
            }
        },
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            data: []
        },
        dataZoom: [{}],
        yAxis: [{
            type: 'value',
            name: 'Delay',
            position: 'left'
        }, {
            type: 'value',
            name: 'Package(LOSS)',
            min: 0,
            max: 100,
            position: 'right',
            axisLabel: {
                formatter: '{value} %'
            }
        }],
        series: [{
            name: 'maxDelay',
            type: 'line',
            animation: false,
            areaStyle: {
                normal: {}
            },
            lineStyle: {
                normal: {
                    width: 0
                }
            },
            data: []
        },
            {
                name: 'minDelay',
                type: 'line',
                animation: false,
                areaStyle: {
                    normal: {}
                },
                lineStyle: {
                    normal: {
                        width: 0
                    }
                },
                data: []
            },
            {
                name: 'avgDelay',
                type: 'line',
                data: [],
                itemStyle: {
                    normal: {
                        color : '#00CC66'
                    }
                },
                animation: false,
                areaStyle: {
                    normal: {}
                },
                lineStyle: {
                    normal: {
                        width: 0
                    }
                }
            },
            {
                name: 'lossPk',
                type: 'line',
                yAxisIndex: 1,
                data: [],
                itemStyle: {
                    normal: {
                        color : '#FF0000'
                    }
                },
                animation: false,
                areaStyle: {
                    normal: {}
                },
                lineStyle: {
                    normal: {
                        width: 0
                    }
                }
            }]
    }
    var myChart = echarts.init(document.getElementById('pannel-show'));
    myChart.setOption(opt);
</script>
</body>
</html>
